const handleTagsToIds = (tagList: Array<any>) => tagList?.map(({ id }: any) => id)
const handleTagsToIdName = (tagList: Array<any>) => tagList?.map(({ id, name }: any) => ({ id, name }))
const handleSwitchToNum = (value: boolean) => (value ? 2 : 1)
const handleNumToBoolean = (value: number) => (value === 2 ? true : false)

// 数据的处理
export const handleFormConfigData = (formDataList: Array<any>) => {
  const form = formDataList.map((formItem: any) => {
    // 基本信息
    const { key, sort, type, form_title, form_desc, form_placeholder, customer_attr_id, is_customer_attr } = formItem
    // 是/否转化为数字
    const { update_customer_message, not_customer_auto_not_weiwork, is_require, is_tags, is_rules } = formItem
    // 列表数据转化
    const { options_list, picture_url, rules_list } = formItem
    return {
      form_value: '',
      key, // 表单提交时用到的字段名称
      sort, // 顺序索引
      type, // 控件类型
      form_title, // 控件标题
      form_desc, // 控件描述
      form_placeholder, // 控件提示语
      customer_attr_id, // 客户属性id
      update_customer_message: +update_customer_message, // 2是或1否， 更新客户信息字段（客户属性才有）
      not_customer_auto_not_weiwork: +not_customer_auto_not_weiwork, // 2是或1否， 非客户自动成为非企微客户
      is_customer_attr, // 2是或1否 是否是客户属性
      is_require: +is_require, // 2是或1否 是否必填
      is_tags: +is_tags, // 2是或1否 是否打标签
      is_rules: +is_rules, // 2是或1否 是否关联规则
      // 下拉、单选、多选等所需字段

      options_list: options_list?.map((optItem: any) => {
        return {
          value: optItem.value,
          tags_list: handleTagsToIdName(optItem.tags_list)
        }
      }) || [],
      // 图片的url
      picture_url: picture_url?.map((picItem: any) => picItem.url) || [],
      // 关联规则
      rules_list: is_rules ? rules_list : []
    }
  })
  return form
}
export const handleFormConfigDataToView = (formDataList: Array<any>) => {
  const form = formDataList.map((formItem: any) => {
    // 基本信息
    const { key, sort, type, form_title, form_desc, form_placeholder, customer_attr_id, is_customer_attr } = formItem
    // 是/否转化为数字
    const { update_customer_message, not_customer_auto_not_weiwork, is_require, is_tags, is_rules } = formItem
    // 列表数据转化
    const { options_list, picture_url, rules_list } = formItem
    return {
      form_value: '',
      id: customer_attr_id || key,
      key, // 表单提交时用到的字段名称
      sort, // 顺序索引
      type, // 控件类型
      form_title, // 控件标题
      form_desc, // 控件描述
      form_placeholder, // 控件提示语
      customer_attr_id, // 客户属性id
      update_customer_message: !!update_customer_message, // 2是或1否， 更新客户信息字段（客户属性才有）
      not_customer_auto_not_weiwork: !!not_customer_auto_not_weiwork, // 2是或1否， 非客户自动成为非企微客户
      is_customer_attr, // 2是或1否 是否是客户属性
      is_require: !!is_require, // 2是或1否 是否必填
      is_tags: !!is_tags, // 2是或1否 是否打标签
      is_rules: !!is_rules, // 2是或1否 是否关联规则
      // 下拉、单选、多选等所需字段

      options_list: options_list?.map((optItem: any) => {
        return {
          value: optItem.value,
          tags_list: handleTagsToIdName(optItem.tags_list)
        }
      }) || [],
      // 图片的url
      picture_url: picture_url?.map((picItem: any) => ({ url: picItem })) || [],
      // 关联规则
      rules_list
    }
  })
  return form
}

// 其他配置数据处理
export const handleOtherConfig = (formData: any, fileList: Array<any>, welcome: any) => {
  // 是/否转化为数字=>标签设置
  const { tags_enter_enable, tags_submit_enable, tags_enter, tags_submit, submit_settings, share_setting } = formData
  // 提交设置
  const { title, code_scanning_guidance, submit_redirect_type, work_user_welcome, group_id, enable } = submit_settings.redirect

  const welcomeInfo = enable && submit_redirect_type === 1 ? welcome : {}
  const limit_value = {
    1: 1,
    2: 0,
    3: submit_settings.limit_value
  }
  const form = {
    tags_enter_enable: handleSwitchToNum(tags_enter_enable),
    tags_enter: handleTagsToIds(tags_enter),
    tags_submit_enable: handleSwitchToNum(tags_submit_enable),
    tags_submit: handleTagsToIds(tags_submit),
    submit_settings: {
      limit_type: submit_settings.limit_type,
      limit_value: limit_value[submit_settings.limit_type],
      // collect_type: submit_settings.collect_type,
      collect_limit: submit_settings.collect_type ? submit_settings.collect_limit : 0,
      redirect: {
        enable: handleSwitchToNum(enable),
        title,
        code_scanning_guidance,
        submit_redirect_type,
        work_user_welcome: {
          ...welcomeInfo,
          work_user_ids: handleTagsToIds(work_user_welcome.work_user_ids)
        },
        group_id: group_id.group_id
      }
    },
    share_setting: {
      ...share_setting,
      link_img_url: fileList[0]?.url,
      link_img_file_id: fileList[0]?.code
    }
  }
  return form
}
// 其他配置数据处理
export const handleOtherConfigToView = (formData: any) => {
  // 是/否转化为数字=>标签设置
  const { tags_enter_enable, tags_submit_enable, tags_enter, tags_submit, submit_settings, share_setting } = formData

  // 提交设置
  const group_info = formData.submit_settings?.redirect?.welcome?.cust_group_info || {}
  const welcome = submit_settings?.redirect?.welcome
  const form = {
    tags_enter_enable: handleNumToBoolean(tags_enter_enable),
    tags_enter: tags_enter,
    tags_submit_enable: handleNumToBoolean(tags_submit_enable),
    tags_submit: tags_submit,
    submit_settings: {
      limit_type: submit_settings.limit_type,
      limit_value: submit_settings.limit_value,
      collect_type: !!submit_settings.collect_limit,
      collect_limit: submit_settings.collect_limit,
      redirect: {
        enable: handleNumToBoolean(submit_settings.redirect.enable),
        title: submit_settings.redirect.title,
        code_scanning_guidance: submit_settings.redirect?.code_scanning_guidance,
        submit_redirect_type: submit_settings.redirect?.submit_redirect_type, // 员工/群聊
        work_user_welcome: {
          work_user_ids: welcome.work_user_list
        },
        group_id: { group_id: group_info.id, name: group_info.name, avatar: welcome.cust_group_avatar, manager: { name: welcome.owner_work_user_name } }
      }
    },
    share_setting: {
      link_img_url: share_setting.link_img_url,
      link_title: share_setting.link_title,
      link_describe: share_setting.link_describe
    }
  }
  return form
}
